package com.orange.cetback.service.impl;

import com.orange.cetback.common.context.BaseContext;
import com.orange.cetback.common.utils.SMT2Util;
import com.orange.cetback.pojo.dto.StudyWordDTO;
import com.orange.cetback.pojo.entity.UserWordReview;
import com.orange.cetback.mapper.UserWordReviewMapper;
import com.orange.cetback.pojo.vo.ReviewWordVO;
import com.orange.cetback.service.UserWordReviewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 基于遗忘曲线的单词复习表 服务实现类
 * </p>
 *
 * @author chenenjun
 * @since 2025-03-01
 */
@Service
public class UserWordReviewServiceImpl extends ServiceImpl<UserWordReviewMapper, UserWordReview> implements UserWordReviewService {

    @Override
    public List<ReviewWordVO> submitStudyWord(List<StudyWordDTO> studyWordDTOS) {
        List<ReviewWordVO> reviewWordVOS = new ArrayList<>();
        for (StudyWordDTO studyWordDTO : studyWordDTOS){
            if (studyWordDTO.getEaseFactor() == null){
                UserWordReview userWordReview = new UserWordReview();
                userWordReview.setUserId(BaseContext.getCurrentId());
                userWordReview.setWordId(studyWordDTO.getWordId());
                SMT2Util.initNewWordReview(userWordReview);
                SMT2Util.updateReviewData(userWordReview, studyWordDTO.getIsCorrect());
                this.save(userWordReview);
                ReviewWordVO reviewWordVO = ReviewWordVO.builder().word(studyWordDTO.getWord()).build();
                BeanUtils.copyProperties(userWordReview, reviewWordVO);
                reviewWordVOS.add(reviewWordVO);
            }else {
                UserWordReview userWordReview = new UserWordReview();
                BeanUtils.copyProperties(studyWordDTO, userWordReview);
                SMT2Util.updateReviewData(userWordReview, studyWordDTO.getIsCorrect());
                this.updateById(userWordReview);
                ReviewWordVO reviewWordVO = ReviewWordVO.builder().word(studyWordDTO.getWord()).build();
                BeanUtils.copyProperties(userWordReview, reviewWordVO);
                reviewWordVOS.add(reviewWordVO);
                reviewWordVOS.add(reviewWordVO);
            }
        }
        return reviewWordVOS;
    }
}
